package serverlogic;

import net.com.sunkey.protobuf.Protocol;
import net.com.sunkey.serverlogic.Message;
import net.com.sunkey.serverlogic.PluginData;
import net.com.sunkey.serverlogic.Room;
import net.com.sunkey.serverlogic.Scheduled;
import net.com.sunkey.serverlogic.Server;
import net.com.sunkey.serverlogic.User;

/**
 * @author kohachiro
 *
 */

public class ScheduledBattle extends Scheduled {
	/**
	 * 
	 */
	public ScheduledBattle() {

	}
	private int getNewSeat(User user) throws Exception {
		int leftSide=0;
		int rightSide=0;
		int x=-1;
		int y=-1;
		for (int i=0;i<3;i++){
			if (getProperty("seat"+i)==null){
				if (x==-1)
					x=i;
				leftSide++;
			}
			if (getProperty("seat"+(i+3))==null){
				if (y==-1)
					y=i+3;				
				rightSide++;
			}
		}
		if (leftSide<rightSide){
			if (y>=0)
				addProperty("seat"+y, String.valueOf(user.getId()));
			return y;
		}else{
			if (x>=0)
				addProperty("seat"+x, String.valueOf(user.getId()));
			return x;
		}
	}
	/* (non-Javadoc)
	 * @see net.com.sunkey.serverlogic.Scheduled#register()
	 */
	@Override
	public int register() {
		return 200;
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		if (getRoom()==null)
			return;
		
		try {
			System.out.println("sendMessage");
			getRoom().sendToRoom(new Message("ScheduledBattle", Server.getSystemUser(), Protocol.MessageRouter.ROUTER_ROOM_VALUE));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onNewUser(User sender) throws Exception {
		if (getProperty("owner")==null)
			addProperty("owner", String.valueOf(getRoom().getCreaterId()));	
		
		int seatId=getNewSeat(sender);
		//System.out.println("seatId:"+seatId);
		PluginData meMessage=new PluginData("PluginBattle","seatList");
		//send owner 
		meMessage.addProperty("owner", getProperty("owner"));
		for (int i=0;i<getRoom().getMaxUsers();i++){
			//0 3 1 4 2 5
			int id=i/2+i%2*3;
			String userId=getProperty("seat"+id);
			if (userId!=null){
				meMessage.addProperty("seat"+id, userId);
				String ready=getProperty("ready"+id);
				if (ready!=null)
					meMessage.addProperty("ready"+id, ready);
				else
					meMessage.addProperty("ready"+id, "0");
			}
		}
		sender.pluginNotice(meMessage);
		if (seatId>=0){
			PluginData message=new PluginData("PluginBattle","newSeat");
			message.addProperty("seat"+seatId, String.valueOf(sender.getId()));			
			getRoom().pluginNotice(sender.getId(),message);
		}
	}

	@Override
	public void onRemoveUser(int userId) throws Exception {
		PluginData message=new PluginData("PluginBattle","removeSeat");
		String ownerId=getProperty("owner");
		//change owner
		if (ownerId!=null&&ownerId.equals(String.valueOf(userId))&&getRoom().getUserList().entrySet().iterator().hasNext()){
			String id = String.valueOf(getRoom().getUserList().entrySet().iterator().next().getValue().getId());
			addProperty("owner", id);
			message.addProperty("owner", id);

		}
		for (int i=0;i<6;i++){
			String uid=getProperty("seat"+i);
			if (uid!=null && uid.equals(String.valueOf(userId))){
				removeProperty("seat"+i);
				removeProperty("ready"+i);

				message.addProperty("seat"+i, "");
				message.addProperty("ready"+i, "0");
				getRoom().pluginNotice(Integer.parseInt(uid),message);
				return;
			}
		}
	}
}
